package io.mft.runtime.function.process.executor;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import io.mft.api.domain.Api;
import io.mft.api.domain.InputParameter;
import io.mft.api.domain.Regulation;
import io.mft.api.domain.InputParameter.InputParameterDataType;
import io.mft.commons.exception.MFTException;
import io.mft.commons.function.ExecuteReturn;
import io.mft.commons.function.response.kv.KVMap;
import io.mft.commons.function.response.kv.KVMapList;
import io.mft.runtime.function.ApiContext;
import io.mft.runtime.function.process.ApiFunctionProcessExecutor;
/**
 * 输入参数校验处理执行器
 * @说明：TODO
 * @类型名称：InputParameterProcessExecutor
 * @创建者: 胡常建  
 * @创建时间: 2021年1月26日 下午4:40:43
 * @修改者: 胡常建  
 * @修改时间: 2021年1月26日 下午4:40:43
 */
public class InputParameterProcessExecutor extends ApiFunctionProcessExecutor<Regulation>{
	Logger logger=LoggerFactory.getLogger(getClass());
	@Override
	public ExecuteReturn doExecute(ApiContext context) {
		List<InputParameter> inputParameters=context.getApiExecutorObject().getApi().getInput().getInputParameters();
		for (InputParameter inputParameter : inputParameters) {
			 String value=context.getRequest().getRequestValue().getStr(inputParameter.getKey());
			 if (ObjectUtil.isEmpty(value)) {
				 if (ObjectUtil.isNotEmpty(inputParameter.getMapping())) {
					 String[] mappingArray=inputParameter.getMapping().split(",");
					 for (int i = 0; i < mappingArray.length; i++) {
						 String mk=mappingArray[i];
						 value=context.getRequest().getRequestValue().getString(mk,"");
						 if (ObjectUtil.isNotEmpty(value)) {
							 context.getRequest().getRequestValue().set(inputParameter.getKey(), value);
							 break;
						 }
					 }
				 }
			 }
			 if (inputParameter.isRequire()) {
				if (ObjectUtil.isEmpty(value)) {
					Api api=context.getApiExecutorObject().getApi();
					throw new MFTException("请求【%s】入参【KEY:%s,名称:%s】不允许为空!",api.getKey(),inputParameter.getKey(),inputParameter.getName()) ;
				}
			}else {
				if (ObjectUtil.isEmpty(value)) {
					value=inputParameter.getDefaultValue();
				}
				context.getRequest().getRequestValue().set(inputParameter.getKey(), value);
			}
			if (ObjectUtil.isNotEmpty(value)) {
				if (InputParameterDataType.json==inputParameter.getDataType()) {
					JSONObject json=JSONUtil.parseObj(value);
					KVMap kv=parse(json, inputParameter, context.getApiExecutorObject().getApi());
					context.getRequest().getRequestValue().set(inputParameter.getKey(), kv);
				} else if (InputParameterDataType.jsonArray==inputParameter.getDataType()) {
					JSONArray jsonArray=JSONUtil.parseArray(value);
					KVMapList kvMapList=new KVMapList();
					for (Object object : jsonArray) {
						JSONObject json=(JSONObject)object;
						KVMap kv=parse(json, inputParameter, context.getApiExecutorObject().getApi());
						kvMapList.add(kv);
					}
					context.getRequest().getRequestValue().set(inputParameter.getKey(), kvMapList);
				}
			}

		}
		
		return ExecuteReturn.builder().build();
	}

	
	private KVMap parse(JSONObject json,InputParameter inputParameter,Api api) {
		KVMap kv=json.toBean(KVMap.class);
		List<InputParameter> jsonKeys=inputParameter.getJsonFormat().getKeys();
		for (InputParameter jsonKey : jsonKeys) {
			 String jsonV=kv.getStr(jsonKey.getKey());
			 if (jsonKey.isRequire()) {
				 if (ObjectUtil.isEmpty(jsonV)) {
						throw new MFTException("请求【%s】入参【KEY:%s,名称:%s】json参数【KEY:%s,名称:%s】不允许为空!",api.getKey(),inputParameter.getKey(),inputParameter.getName(),jsonKey.getKey(),jsonKey.getName()) ;
				}
			}else {
				if (ObjectUtil.isEmpty(jsonV)) {
					jsonV=jsonKey.getDefaultValue();
				}
				kv.put(jsonKey.getKey(), jsonV);
			}
		}
		return kv;
	}
}
